package com.oceansoft.mobile.econsole.task.sms;

import org.apache.commons.lang.StringUtils;

import java.text.MessageFormat;
import java.util.*;
import java.util.regex.Pattern;

/**
 * 字符串处理类
 *
 * @author Administrator 方法名 方法用途 isWord(String) 判断传入的String是否是? 没有测试实际效果 不推荐使用
 *         isNumEx(String) 判断输入是否是>=0的数字 getStringNumber(String str, int index)
 *         2321adasd2321asdsa2321 2 返回结果为2321 sortByLength(String[] ,boolean)
 *         按数组里的每个单词的长度进行排序 isEmailAddress(String) 判断是否为email格式字符串
 *         quoteNullString(String) 非空字符串外面包单引号
 *         getCharAtPosDefaultZero(String,int) 返回指定位置的char 默认为0
 *         setCharAtPosAppendZero(String s, int pos, char c) 在指定位置加入指定char
 *         fillBlank(String s, int n, boolean isLeft) 在指定位置的左边或右边加入空格
 *         fillPrefix(String s, int n,String prefix, boolean isLeft)
 *         在字符串s的左边或右边补全prefix到字符n位 compareVersion(String version1, String
 *         version2) 从左到右 前者要是所有位置都和后者一直 但是少几位则为 -1 完全一直为0 ,多余或不一样则为1
 *         deleteAny(String s1,String s2) s1中去掉所有s2
 *         removeDuplicateStrings(String []) 去掉数组中的重复对象
 *         splitArrayElementsIntoProperties(String [] t,String s)
 *         将t中的每个字符串按照s进行分割 多个s的话 去前两个 最后返回properties
 *         splitArrayElementsIntoProperties(String [] t,String s,String del)
 *         将t中的每个字符串先进行去除del操作后进行按s分割 多个s的话 去前两个 最后返回properties
 *         tokenizeToStringArray(String s,String d) s以d进行分割 返回string[]
 *         commaDelimitedListToStringArray 按逗号分割成String[]
 *         commaDelimitedListToSet 按逗号分割成set arrayToDelimitedString
 *         (Object[],String s) 数组变成string 用s连接 collectionToDelimitedString
 *         (collection , delim,perfix ,subfix ) collection 按delim 连接 并且加上前缀和后缀
 *         hasWildcards(String input) 判断input中是否含有?或* isIN(String r_Source,
 *         Colloection r_Target) r_Target中是否存在r_Source ,忽略大小写 getUUID(String...)
 *         获取随机uuid toBoolean(String str,boolean b) str==null 则返回b,否则判断str是否是{
 *         "TRUE", "1", "Y", "YES" };中的一种 map2String(Map) 格式为{key=value}
 */
public final class StringUtil {

    public final static int CASETYPE_LOWER = 0;
    public final static int CASETYPE_UPPER = 1;
    static final String[] SBC = {"，", "。", "；", "“", "”", "？", "！", "（", "）", "：", "——", "、"};
    static final String[] DBC = {",", ".", ";", "\"", "\"", "?", "!", "(", ")", ":", "_", ","};
    static final String[] TRUESTR = {"TRUE", "1", "Y", "YES"};

    private static final String EMAIL_PATTERN = "^[_a-z0-9-]+(\\.[_a-z0-9-]+)*@[a-z0-9-]+(\\.[a-z0-9-]+)*(\\.[a-z]{2,3})$";
    private static final transient Pattern emailPattern;
    private static final String DEFAULT_PREFIX = "${";
    private static final String DEFAULT_SUFFIX = "}";

    static {
        emailPattern = Pattern.compile(EMAIL_PATTERN);
    }

    // 没有测试出预期结果 ,不推荐使用
    public static boolean isWord(String str) {
        if (str == null) {
            return false;
        }

        char[] ch = str.toCharArray();

        for (int i = 0; i < str.length(); ++i) {
            if ((!(Character.isLetterOrDigit(ch[i]))) && (ch[i] != '_')) {
                return false;
            }
        }
        return true;
    }

    /**
     * 大于等于0的数字返回true
     *
     * @param str
     * @return
     */
    public static boolean isNumEx(String str) {
        if ((str == null) || (str.length() <= 0)) {
            return false;
        }

        char[] ch = str.toCharArray();

        int i = 0;
        for (int comcount = 0; i < str.length(); ++i) {
            if (!(Character.isDigit(ch[i]))) {
                if (ch[i] != '.') {
                    return false;
                }
                if ((i == 0) || (i == str.length() - 1)) {
                    return false;
                }
                if (++comcount > 1) {
                    return false;
                }
            }
        }
        return true;
    }

    /**
     * 2321adasd2321asdsa2321 2 返回结果为2321 返回数字串
     *
     * @param str
     * @param index
     * @return
     */
    public static Object getStringNumber(String str, int index) {
        if (str == null) {
            return null;
        }

        char[] ch = str.toCharArray();

        String tempStr = "";
        Vector<String> returnNumber = new Vector<String>();

        for (int i = 0; i < str.length(); ++i) {
            if (Character.isDigit(ch[i])) {
                tempStr = tempStr + ch[i];
            } else {
                if (!(tempStr.equals(""))) {
                    returnNumber.addElement(tempStr);
                }
                tempStr = "";
            }
        }

        if (!(tempStr.equals(""))) {
            returnNumber.addElement(tempStr);
        }

        if ((returnNumber.isEmpty()) || (index > returnNumber.size())) {
            return null;
        }

        if (index <= 0) {
            return returnNumber;
        }

        return returnNumber.elementAt(index - 1);
    }

    /**
     * 按数组中每个字符串的长度重新排序
     *
     * @param saSource
     * @param bAsc
     * @return
     */
    public static String[] sortByLength(String[] saSource, boolean bAsc) {
        if ((saSource == null) || (saSource.length <= 0)) {
            return null;
        }

        int iLength = saSource.length;
        String[] saDest = new String[iLength];

        for (int i = 0; i < iLength; ++i) {
            saDest[i] = saSource[i];
        }

        String sTemp = "";
        int j = 0;
        int k = 0;

        for (j = 0; j < iLength; ++j) {
            for (k = 0; k < iLength - j - 1; ++k) {
                if ((saDest[k].length() > saDest[(k + 1)].length()) && (bAsc)) {
                    sTemp = saDest[k];
                    saDest[k] = saDest[(k + 1)];
                    saDest[(k + 1)] = sTemp;
                } else if ((saDest[k].length() < saDest[(k + 1)].length()) && (!(bAsc))) {
                    sTemp = saDest[k];
                    saDest[k] = saDest[(k + 1)];
                    saDest[(k + 1)] = sTemp;
                }
            }
        }
        return saDest;
    }

    /**
     * 中文标点转换为英文标点
     *
     * @param sSource
     * @return
     */
    public static String symbolSBCToDBC(String sSource) {
        if ((sSource == null) || (sSource.length() <= 0)) {
            return sSource;
        }

        int iLen = (SBC.length < DBC.length) ? SBC.length : DBC.length;
        for (int i = 0; i < iLen; ++i) {
            sSource = StringUtils.replace(sSource, SBC[i], DBC[i]);
        }
        return sSource;
    }

    /**
     * 英文标点转换为中文标点
     *
     * @param sSource
     * @return
     */
    public static String symbolDBCToSBC(String sSource) {
        if ((sSource == null) || (sSource.length() <= 0)) {
            return sSource;
        }

        int iLen = (SBC.length < DBC.length) ? SBC.length : DBC.length;
        for (int i = 0; i < iLen; ++i) {
            sSource = StringUtils.replace(sSource, DBC[i], SBC[i]);
        }
        return sSource;
    }

    /**
     * 判断是否是email格式的字符串
     *
     * @param str
     * @return
     */
    public static boolean isEmailAddress(String str) {
        if (StringUtils.isEmpty(str)) {
            return false;
        }

        return emailPattern.matcher(str).matches();
    }

    /**
     * 为非空字符串包个单引号在外面,为空则返回Null
     *
     * @param s
     * @return
     */
    public static String quoteNullString(String s) {
        if (s == null) {
            return "Null";
        }
        if (s.trim().length() == 0) {
            return "Null";
        }
        return "'" + s.trim() + "'";
    }

    /**
     * 或者字符中pos位置的char
     *
     * @param s
     * @param pos
     * @return
     */
    public static char getCharAtPosDefaultZero(String s, int pos) {
        if (s == null) {
            return '0';
        }

        if (s.length() <= pos) {
            return '0';
        }
        return s.charAt(pos);
    }

    /**
     * 在字符串s中pos位置添加c
     *
     * @param s
     * @param pos
     * @param c
     * @return
     */
    public static String setCharAtPosAppendZero(String s, int pos, char c) {
        if (s == null) {
            s = "";
        }

        while (pos > s.length() - 1)
            s = s + '0';
        String preString;
        if (pos == 0) {
            preString = "";
        } else
            preString = s.substring(0, pos);
        String afterString;
        if (pos == s.length() - 1) {
            afterString = "";
        } else {
            afterString = s.substring(pos + 1);
        }

        return preString + c + afterString;
    }

    /**
     * 在字符串s的左边或右边补全prefix到字符n位
     *
     * @param s
     * @param n
     * @param isLeft
     * @return
     */
    public static String fillPrefix(String s, int n, String prefix, boolean isLeft) {
        if (n < 0) {
            return s;
        }

        if (StringUtils.isEmpty(s)) {
            return StringUtils.rightPad("", n, prefix);
        }

        if (s.length() >= n) {
            return s;
        }
        if (isLeft) {
            return StringUtils.leftPad(s, n, prefix);
        }

        return StringUtils.rightPad(s, n, prefix);
    }

    /**
     * 在字符串s的n位置的左边或右边添加一个空格
     *
     * @param s
     * @param n
     * @param isLeft
     * @return
     */
    public static String fillBlank(String s, int n, boolean isLeft) {
        if (n < 0) {
            return s;
        }

        if (StringUtils.isEmpty(s)) {
            return StringUtils.rightPad("", n, " ");
        }

        if (s.length() >= n) {
            return s;
        }
        if (isLeft) {
            return StringUtils.leftPad(s, n, " ");
        }

        return StringUtils.rightPad(s, n, " ");
    }

    /**
     * 从左到右 前者要是所有位置都和后者一直 但是少几位则为 -1 完全一直为0 ,多余或不一样则为1
     *
     * @param version1
     * @param version2
     * @return
     */
    public static int compareVersion(String version1, String version2) {
        StringTokenizer st1 = new StringTokenizer(version1, ".");
        StringTokenizer st2 = new StringTokenizer(version2, ".");

        ArrayList<String> al1 = new ArrayList<String>();
        ArrayList<String> al2 = new ArrayList<String>();

        while (st1.hasMoreTokens()) {
            al1.add(st1.nextToken());
        }
        while (st2.hasMoreTokens()) {
            al2.add(st2.nextToken());
        }

        int size1 = al1.size();
        int size2 = al2.size();

        for (int i = 0; (i < size1) && (i < size2); ++i) {
            int v1 = Integer.parseInt(al1.get(i));
            int v2 = Integer.parseInt(al2.get(i));

            if (v1 > v2) {
                return 1;
            }
            if (v1 < v2) {
                return -1;
            }
        }

        if (size1 > size2) {
            return 1;
        }
        if (size1 < size2) {
            return -1;
        }
        return 0;
    }

    /**
     * 从字符串inString中去掉所有的charsToDelete
     *
     * @param inString
     * @param charsToDelete
     * @return
     */
    public static String deleteAny(String inString, String charsToDelete) {
        if ((inString == null) || (charsToDelete == null)) {
            return inString;
        }

        StringBuffer out = new StringBuffer();
        for (int i = 0; i < inString.length(); ++i) {
            char c = inString.charAt(i);
            if (charsToDelete.indexOf(c) == -1) {
                out.append(c);
            }
        }
        return out.toString();
    }

    /**
     * 为非空字符串包个单引号在外面
     *
     * @param s
     * @return
     */
    public static String quote(String str) {
        return ((str != null) ? "'" + str + "'" : null);
    }

    /**
     * obj是String 则返回quote(obj)
     *
     * @param obj
     * @return
     */
    public static Object quoteIfString(Object obj) {
        return ((obj instanceof String) ? quote((String) obj) : obj);
    }

    /**
     * 获取qualifiedName最后出现.的前面部分
     *
     * @param qualifiedName
     * @param separator
     * @return
     */
    public static String unqualify(String qualifiedName) {
        return unqualify(qualifiedName, '.');
    }

    /**
     * 获取qualifiedName最后出现separator的前面部分
     *
     * @param qualifiedName
     * @param separator
     * @return
     */
    public static String unqualify(String qualifiedName, char separator) {
        return qualifiedName.substring(qualifiedName.lastIndexOf(separator) + 1);
    }

    /**
     * 对数组中每个字符串按delimiter分割后返回properties
     *
     * @param array
     * @param delimiter
     * @return
     */
    public static Properties splitArrayElementsIntoProperties(String[] array, String delimiter) {
        return splitArrayElementsIntoProperties(array, delimiter, null);
    }

    /**
     * 出去charsToDelete 后 对数组中每个字符串按delimiter分割后返回properties
     *
     * @param array
     * @param delimiter
     * @param charsToDelete
     * @return
     */
    public static Properties splitArrayElementsIntoProperties(String[] array, String delimiter, String charsToDelete) {
        if ((array == null) || (array.length == 0)) {
            return null;
        }

        Properties result = new Properties();
        for (int i = 0; i < array.length; ++i) {
            String element = array[i];
            if (charsToDelete != null) {
                element = deleteAny(array[i], charsToDelete);
            }
            String[] splittedElement = StringUtils.split(element, delimiter);
            if (splittedElement == null) {
                continue;
            }
            result.setProperty(splittedElement[0].trim(), splittedElement[1].trim());
        }
        return result;
    }

    /**
     * 按逗号分割成string[]
     *
     * @param str
     * @return
     */
    public static String[] commaDelimitedListToStringArray(String str) {
        return StringUtils.split(str, ",");
    }

    /**
     * 按逗号分割成 返回set
     *
     * @param str
     * @return
     */
    public static Set<String> commaDelimitedListToSet(String str) {
        Set<String> set = new TreeSet<String>();
        String[] tokens = commaDelimitedListToStringArray(str);
        for (int i = 0; i < tokens.length; ++i) {
            set.add(tokens[i]);
        }
        return set;
    }

    /**
     * 数组用delim连接成字符串
     *
     * @param arr
     * @param delim
     * @return
     */
    public static String arrayToDelimitedString(Object[] arr, String delim) {
        if (arr == null) {
            return "";
        }

        StringBuffer sb = new StringBuffer();
        for (int i = 0; i < arr.length; ++i) {
            if (i > 0) {
                sb.append(delim);
            }
            sb.append(arr[i]);
        }
        return sb.toString();
    }

    /**
     * Collection 用delim 连接 并加上前缀和后缀
     *
     * @param coll
     * @param delim
     * @param prefix
     * @param suffix
     * @return
     */
    public static String collectionToDelimitedString(Collection<?> coll, String delim, String prefix, String suffix) {
        if (coll == null) {
            return "";
        }

        StringBuffer sb = new StringBuffer();
        Iterator<?> it = coll.iterator();
        int i = 0;
        while (it.hasNext()) {
            if (i > 0) {
                sb.append(delim);
            }
            sb.append(prefix).append(it.next()).append(suffix);
            ++i;
        }
        return sb.toString();
    }

    /**
     * Collection 用delim 连接
     *
     * @param coll
     * @param delim
     * @return
     */
    public static String collectionToDelimitedString(Collection<?> coll, String delim) {
        return collectionToDelimitedString(coll, delim, "", "");
    }

    /**
     * 数组用","连接成字符串
     *
     * @param arr
     * @return
     */
    public static String arrayToCommaDelimitedString(Object[] arr) {
        return arrayToDelimitedString(arr, ",");
    }

    /**
     * Collection 用逗号连接成字符串
     *
     * @param coll
     * @return
     */
    public static String collectionToCommaDelimitedString(Collection<?> coll) {
        return collectionToDelimitedString(coll, ",");
    }

    // 没搞明白map怎么用
    public static String replace(String src, Map<?, ?> props) {
        return replace(src, DEFAULT_PREFIX, DEFAULT_SUFFIX, props);
    }

    // 没搞明白map怎么用
    public static String replace(String src, String prefix, String suffix, Map<?, ?> props) {
        int len1 = prefix.length();
        int len2 = suffix.length();

        StringBuffer sb = new StringBuffer();

        int index1 = src.indexOf(prefix);
        while (true) {
            if (index1 < 0) {
                sb.append(src);
                break;
            }
            sb.append(src.substring(0, index1));
            src = src.substring(index1 + len1);
            if (src.startsWith(prefix)) {
                sb.append(prefix);
                sb.append(src);
                break;
            }

            int index2 = src.indexOf(suffix);
            if (index2 < 0) {
                if (" ".equals(suffix)) {
                    Object o = props.get(src);
                    String sp = (o == null) ? "" : o.toString();
                    sb.append(sp);
                    sb.append(" ");
                } else {
                    sb.append(prefix);
                }
                break;
            }

            String t = src.substring(0, index2);
            Object o = props.get(t);
            String sp = (o == null) ? "" : o.toString();
            sb.append(sp);
            if (" ".equals(suffix)) {
                sb.append(" ");
            }
            src = src.substring(index2 + len2);
            index1 = src.indexOf(prefix);
        }

        return new String(sb);
    }

    /**
     * 判断不为空 包含null,"",以及"null"
     *
     * @param str
     * @return
     */
    public static boolean isNotNullAndBlank(String str) {
        return (!(isNullOrBlank(str)));
    }

    /**
     * 判断为空 包含null,"",以及"null"
     *
     * @param str
     * @return
     */
    public static boolean isNullOrBlank(String str) {
        return ((isNull(str)) || (str.equals("")) || (str.equals("null")));
    }

    /**
     * 判断字符串是否为空，包括null和""
     *
     * @param str 待判断的字符。
     * @return
     */
    public static boolean isNull(String str) {
        return ((str == null) || (str.trim().length() == 0));
    }

    /**
     * 判断是否不为空 包括null和""
     *
     * @param str
     * @return
     */
    public static boolean isNotNull(String str) {
        if ((str == null) || (str.trim().length() == 0)) {
            return false;
        }

        return (!(str.trim().equalsIgnoreCase("null")));
    }

    /**
     * 如果是null 则返回"" 否则trim
     *
     * @param str
     * @return
     */
    public static String ifNullToBlank(String str) {
        if ((isNotNull(str)) && (!(str.trim().equals("null")))) {
            return str.trim();
        }

        return "";
    }

    /**
     * 如果是null 则返回"" 否则trim
     *
     * @param str
     * @return
     */
    public static String ifNullToBlank(Object obj) {
        String ret = "";
        String s = String.valueOf(obj);
        if ((s == null) || ("".equals(s)) || ("null".equals(s)) || ("NULL".equals(s))) {
            ret = "";
        } else {
            ret = s;
        }
        return ret;
    }

    /**
     * input里面是否包含*或?
     *
     * @param input
     * @return
     */
    public static boolean hasWildcards(String input) {
        return ((StringUtils.contains(input, "*")) || (StringUtils.contains(input, "?")));
    }

    /**
     * r_Keyword中是否含有r_WildcardMatcher中的字符串 ,r_CaseSensitive是否忽略大小写
     *
     * @param r_Keyword
     * @param r_WildcardMatcher
     * @param r_CaseSensitive
     * @return
     */
    public static boolean isWildcardMatchOne(String r_Keyword, String[] r_WildcardMatcher, boolean r_CaseSensitive) {
        if (null == r_WildcardMatcher) {
            return true;
        }

        for (int i = 0; i < r_WildcardMatcher.length; ++i) {
            String t_WildCardMatcher = r_WildcardMatcher[i];

            if (isWildcardMatch(r_Keyword, t_WildCardMatcher, r_CaseSensitive)) {
                return true;
            }
        }

        return false;
    }

    public static boolean isWildcardMatchAll(String r_Keyword, String[] r_WildcardMatcher, boolean r_CaseSensitive) {
        if (null == r_WildcardMatcher) {
            return true;
        }

        for (int i = 0; i < r_WildcardMatcher.length; ++i) {
            String t_WildCardMatcher = r_WildcardMatcher[i];

            if (!(isWildcardMatch(r_Keyword, t_WildCardMatcher, r_CaseSensitive))) {
                return false;
            }
        }

        return true;
    }

    public static boolean isWildcardMatch(String r_Keyword, String r_WildcardMatcher) {
        return isWildcardMatch(r_Keyword, r_WildcardMatcher, true);
    }

    public static boolean isWildcardMatch(String r_Keyword, String r_WildcardMatcher, boolean r_CaseSensitive) {
        if ((r_Keyword == null) && (r_WildcardMatcher == null)) {
            return true;
        }
        if ((r_Keyword == null) || (r_WildcardMatcher == null)) {
            return false;
        }
        if (!(r_CaseSensitive)) {
            r_Keyword = r_Keyword.toLowerCase();
            r_WildcardMatcher = r_WildcardMatcher.toLowerCase();
        }
        String[] t_SplitValues = splitOnTokens(r_WildcardMatcher);
        boolean t_Chars = false;
        int t_Index = 0;
        int t_WildIndex = 0;
        Stack<int[]> t_BackStack = new Stack<int[]>();
        do {
            if (t_BackStack.size() > 0) {
                int[] array = t_BackStack.pop();
                t_WildIndex = array[0];
                t_Index = array[1];
                t_Chars = true;
            }

            while (t_WildIndex < t_SplitValues.length) {
                if (t_SplitValues[t_WildIndex].equals("?")) {
                    ++t_Index;
                    t_Chars = false;
                } else if (t_SplitValues[t_WildIndex].equals("*")) {
                    t_Chars = true;
                    if (t_WildIndex == t_SplitValues.length - 1) {
                        t_Index = r_Keyword.length();
                    }

                } else {
                    if (t_Chars) {
                        t_Index = r_Keyword.indexOf(t_SplitValues[t_WildIndex], t_Index);
                        if (t_Index == -1) {
                            break;
                        }

                        int repeat = r_Keyword.indexOf(t_SplitValues[t_WildIndex], t_Index + 1);
                        if (repeat >= 0) {
                            t_BackStack.push(new int[]{t_WildIndex, repeat});
                        }

                    } else {
                        if (!(r_Keyword.startsWith(t_SplitValues[t_WildIndex], t_Index))) {
                            break;
                        }

                    }

                    t_Index += t_SplitValues[t_WildIndex].length();
                    t_Chars = false;
                }

                ++t_WildIndex;
            }

            if ((t_WildIndex == t_SplitValues.length) && (t_Index == r_Keyword.length())) {
                return true;
            }
        } while (t_BackStack.size() > 0);

        return false;
    }

    private static String[] splitOnTokens(String r_Text) {
        if ((r_Text.indexOf("?") == -1) && (r_Text.indexOf("*") == -1)) {
            return new String[]{r_Text};
        }

        char[] t_Array = r_Text.toCharArray();
        ArrayList<String> t_List = new ArrayList<String>();
        StringBuffer t_Buffer = new StringBuffer();
        for (int i = 0; i < t_Array.length; ++i) {
            if ((t_Array[i] == '?') || (t_Array[i] == '*')) {
                if (t_Buffer.length() != 0) {
                    t_List.add(t_Buffer.toString());
                    t_Buffer.setLength(0);
                }
                if (t_Array[i] == '?') {
                    t_List.add("?");
                } else if ((t_List.size() == 0) || ((i > 0) && (!(t_List.get(t_List.size() - 1).equals("*")))))
                    t_List.add("*");
            } else {
                t_Buffer.append(t_Array[i]);
            }
        }
        if (t_Buffer.length() != 0) {
            t_List.add(t_Buffer.toString());
        }

        return t_List.toArray(new String[0]);
    }

    /**
     * r_Target中是否存在r_Source ,r_CaseSensitive是否忽略大小写
     *
     * @param r_Source
     * @param r_Target
     * @param r_CaseSensitive
     * @return
     */
    public static boolean isIn(String r_Source, String[] r_Target, boolean r_CaseSensitive) {
        for (int i = 0; i < r_Target.length; ++i) {
            String t_Value = r_Target[i];
            if (r_CaseSensitive) {
                if (StringUtils.equals(r_Source, t_Value)) {
                    return true;
                }
            } else if (StringUtils.equalsIgnoreCase(r_Source, t_Value)) {
                return true;
            }

        }

        return false;
    }

    /**
     * r_Target中是否存在r_Source ,忽略大小写
     *
     * @param r_Source
     * @param r_Target
     * @param r_CaseSensitive
     * @return
     */
    public static boolean isIn(String r_Source, Collection<?> r_Target) {
        for (Iterator<?> t_Iterator = r_Target.iterator(); t_Iterator.hasNext(); ) {
            String t_Value = (String) t_Iterator.next();
            if (StringUtils.equalsIgnoreCase(r_Source, t_Value)) {
                return true;
            }
        }

        return false;
    }

    public static String targetEndStyle(String name) {
        return "</" + name + ">";
    }

    public static String valueToSetStyle(String value) {
        if (value == null) {
            value = "";
        }
        return "=\"" + value + "\"";
    }

    /**
     * trim后是否相等
     *
     * @param s1
     * @param s2
     * @return
     */
    public static boolean equal(String s1, String s2) {
        if (s1 == s2) {
            return true;
        }

        if (s1 == null) {
            s1 = "";
        }

        if (s2 == null) {
            s2 = "";
        }

        s1 = s1.trim();
        s2 = s2.trim();

        return (s1.equals(s2));
    }

    /**
     * 把args中所有对象连成string
     *
     * @param args
     * @return
     */
    public static String concat(Object[] args) {
        StringBuffer buf = new StringBuffer();
        for (Object arg : args) {
            buf.append(arg);
        }
        return buf.toString();
    }

    public static String format(String s, Object[] params) {
        String message = s;
        if (message == null) {
            return "";
        }
        if ((params != null) && (params.length > 0)) {
            message = new MessageFormat(message).format(params);
        }
        return message;
    }

    /**
     * 是否以prefix为起始
     *
     * @param str
     * @param prefix
     * @return
     */
    public static boolean startsWithIgnoreCase(String str, String prefix) {
        if ((str == null) || (prefix == null)) {
            return false;
        }
        if (str.startsWith(prefix)) {
            return true;
        }
        if (str.length() < prefix.length()) {
            return false;
        }
        String lcStr = str.substring(0, prefix.length()).toLowerCase();
        String lcPrefix = prefix.toLowerCase();
        return lcStr.equals(lcPrefix);
    }

    /**
     * 获取随即uuid
     *
     * @param arg
     * @return
     */
    public static String getUUID(String... arg) {
        String uid = UUID.randomUUID().toString();
        if (arg.length > 0) {
            uid = uid.replaceAll("-", arg[0]);
        }
        return uid;
    }

    public static List<String> splitString(String str) {
        return splitString(str, ",");
    }

    /**
     * 按照seperator 分割成list
     *
     * @param str
     * @param seperator
     * @return
     */
    public static List<String> splitString(String str, String seperator) {
        if (str == null) {
            return new ArrayList<String>(1);
        }
        if (seperator == null) {
            seperator = ",";
        }
        ArrayList<String> retlist = new ArrayList<String>(5);
        if (seperator.length() == 0) {
            retlist.add(str);
            return retlist;
        }
        int sepLen = seperator.length();
        int pos = str.indexOf(seperator);
        while (pos != -1) {
            retlist.add(str.substring(0, pos));
            str = str.substring(pos + sepLen, str.length());
            pos = str.indexOf(seperator);
        }
        retlist.add(str);

        return retlist;
    }

    /**
     * 判断字符串中，第n个字母是否大写字母
     *
     * @param str
     * @param pos
     * @return
     */
    public static boolean isUpperCase(String str, int pos) {
        return isCaseByType(str, pos, CASETYPE_UPPER);
    }

    /**
     * 判断字符串中，第n个字母是否小写字母
     *
     * @param str
     * @param pos
     * @return
     */
    public static boolean isLowerCase(String str, int pos) {
        return isCaseByType(str, pos, CASETYPE_LOWER);
    }

    /**
     * 判断字符串中，第n个字母是否小写字母
     *
     * @param str
     * @param pos
     * @param caseType
     * @return
     */
    public static boolean isCaseByType(String str, int pos, int caseType) {
        char c = getCharAtPosDefaultZero(str, pos);
        if (c == '0') {
            return false;
        } else {
            switch (caseType) {
                case CASETYPE_LOWER:
                    return isLowerCase(c);
                case CASETYPE_UPPER:
                    return isUpperCase(c);
                default:
                    return false;
            }
        }
    }

    public static String upperCaseAtPos(String str, int pos) {
        return changeCaseAtPosByCaseType(str, pos, CASETYPE_UPPER);
    }

    public static String lowerCaseAtPos(String str, int pos) {
        return changeCaseAtPosByCaseType(str, pos, CASETYPE_LOWER);
    }

    public static String changeCaseAtPosByCaseType(String str, int pos, int caseType) {
        char c = getCharAtPosDefaultZero(str, pos);
        if (c == '0') {
            return str;
        } else {
            char newC = c;
            switch (caseType) {
                case CASETYPE_LOWER:
                    newC = toLowerCaseAtPos(c);
                    break;
                case CASETYPE_UPPER:
                    newC = toUpperCase(c);
                    break;
                default:
                    break;
            }
            return setCharAtPosAppendZero(str, pos, newC);
        }
    }

    private static char toLowerCaseAtPos(char c) {
        return Character.toLowerCase(c);
    }

    private static char toUpperCase(char c) {
        return Character.toUpperCase(c);
    }

    private static boolean isLowerCase(char c) {
        if (Character.isLowerCase(c)) {
            return true;
        }
        return false;
    }

    private static boolean isUpperCase(char c) {
        if (Character.isUpperCase(c)) {
            return true;
        }
        return false;
    }

    public static boolean toBoolean(String str) {
        return toBoolean(str, false);
    }

    /**
     * 判断str是否是{ "TRUE", "1", "Y", "YES" };中的一种
     *
     * @param str
     * @param defaultFlag
     * @return
     */
    public static boolean toBoolean(String str, boolean defaultFlag) {
        if (isNull(str)) {
            return defaultFlag;
        } else {
            String temp = str.toUpperCase();
            for (String trueStr : TRUESTR) {
                if (trueStr.equals(temp)) {
                    return true;
                }
            }
        }
        return false;
    }

    /**
     * map对象转换为String 格式为{key=value}
     *
     * @param map
     * @return
     */
    public static String map2String(Map<String, Object> map) {
        StringBuffer sb = new StringBuffer();
        try {
            Iterator<String> iter = map.keySet().iterator();
            while (iter.hasNext()) {
                String key = iter.next();
                String value = map.get(key) + "";
                sb.append("{").append(key).append("=").append(value).append("}");
            }
        } catch (Exception e) {
            e.printStackTrace();
            return sb.toString();
        }
        return sb.toString();
    }
}
